import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:lazy_load_indexed_stack/lazy_load_indexed_stack.dart';
import '../shared/providers/bottom_nav_provider.dart';
import '../features/chat/pages/chat_page.dart';
import '../features/contacts/pages/contacts_page.dart';
import '../features/profile/pages/profile_page.dart';
import '../pages/home/home_tab_page.dart';

class MainLayout extends StatelessWidget {
  const MainLayout({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer<BottomNavProvider>(
      builder: (context, bottomNavProvider, child) {
        return Scaffold(
          body: LazyLoadIndexedStack(
            index: bottomNavProvider.currentIndex,
            children: const [
              HomeTabPage(),      // 索引 0: 聊天（显示聊天列表）
              ContactsPage(),     // 索引 1: 联系人（显示好友列表）
              ChatPage(),         // 索引 2: 发现（待实现）
              ProfilePage(),      // 索引 3: 我的
            ],
          ),
          bottomNavigationBar: Container(
            decoration: BoxDecoration(
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.1),
                  blurRadius: 10,
                  offset: const Offset(0, -2),
                ),
              ],
            ),
            child: BottomNavigationBar(
              currentIndex: bottomNavProvider.currentIndex,
              onTap: (index) {
                bottomNavProvider.setCurrentIndex(index);
              },
              type: BottomNavigationBarType.fixed,
              showSelectedLabels: true,
              showUnselectedLabels: true,
              selectedItemColor: Theme.of(context).colorScheme.primary,
              unselectedItemColor: Colors.grey[600],
              selectedLabelStyle: const TextStyle(
                fontSize: 11,
                fontWeight: FontWeight.w500,
                letterSpacing: 0.5,
              ),
              unselectedLabelStyle: const TextStyle(
                fontSize: 11,
                fontWeight: FontWeight.normal,
                letterSpacing: 0.5,
              ),
              backgroundColor: Theme.of(context).scaffoldBackgroundColor,
              elevation: 0,
              iconSize: 24, // 统一图标大小，避免选中时变大
              items: [
                BottomNavigationBarItem(
                  icon: Icon(
                    Icons.chat_bubble_outline,
                    size: 24,
                  ),
                  activeIcon: Icon(
                    Icons.chat,
                    size: 24,
                  ),
                  label: '聊天',
                ),
                BottomNavigationBarItem(
                  icon: Icon(
                    Icons.people_outline,
                    size: 24,
                  ),
                  activeIcon: Icon(
                    Icons.people,
                    size: 24,
                  ),
                  label: '联系人',
                ),
                BottomNavigationBarItem(
                  icon: Icon(
                    Icons.explore_outlined,
                    size: 24,
                  ),
                  activeIcon: Icon(
                    Icons.explore,
                    size: 24,
                  ),
                  label: '发现',
                ),
                BottomNavigationBarItem(
                  icon: Icon(
                    Icons.person_outline,
                    size: 24,
                  ),
                  activeIcon: Icon(
                    Icons.person,
                    size: 24,
                  ),
                  label: '我的',
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
